﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DoubleH.Utility;
using Table = FCNS.Data.Table;
using System.Data;
using FCNS.Data;
using System.Diagnostics;

namespace ReportS
{
    /// <summary>
    /// 故障库.xaml 的交互逻辑
    /// </summary>
    public partial class 故障库 : Window
    {
        public 故障库()
        {
            InitializeComponent();

            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                return;

            InitVar();
            InitEvent();
        }

        private void InitVar()
        {
            uCdataGrid1.TableText = DataTableText.故障库;
            uCdataGrid1.checkBoxChart.IsChecked = false;
            uCdataGrid1.checkBoxChart.IsEnabled = false;

            labelEnable.Content = "维修对象:";
            checkComboBoxEnable.ItemsSource = Table.UniqueS.GetList(Table.UniqueS.EnumFlag.维修对象);
            checkComboBoxEnable.DisplayMemberPath = "Name";
            checkComboBoxEnable.ValueMemberPath = "Id";

            labelFlag.Content = "维修类型:";
            checkComboBoxFlag.ItemsSource = Table.UniqueS.GetList(Table.UniqueS.EnumFlag.维修类型);
            checkComboBoxFlag.DisplayMemberPath = "Name";
            checkComboBoxFlag.ValueMemberPath = "Id";

            DataGridTextColumn gc3 = new DataGridTextColumn();
            gc3.Header = "故障描述";
            gc3.Binding = new Binding("[GuZhangMiaoShu]");
            uCdataGrid1.dataGridExt1.Columns.Add(gc3);
            DataGridTextColumn gc4 = new DataGridTextColumn();
            gc4.Header = "解决方法";
            gc4.Binding = new Binding("[JieJueFangFa]");
            uCdataGrid1.dataGridExt1.Columns.Add(gc4);
        }

        private void InitEvent()
        {
            buttonOK.Click += (ss, ee) => GetData();
        }

        private void GetData()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select GuZhangMiaoShu,JieJueFangFa,CorSId from AfterSaleServiceS where ");
            sb.Append("(cast(OrderDateTime as varchar)>='" + uCDateTime1.StartDateTime.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "'");
            sb.Append(" and cast(OrderDateTime as varchar)<='" + uCDateTime1.EndDateTime.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "')");

            if (!string.IsNullOrEmpty(checkComboBoxFlag.Text))
            {
                sb.Append(" and (");
                foreach (string str in checkComboBoxFlag.SelectedValue.Split(','))
                    sb.Append("WeiXiuLeiXing=" + (int)Enum.Parse(typeof(Table.PurchaseOrderS.EnumFlag), str) + " or ");

                sb.Remove(sb.Length - 3, 3);
                sb.Append(")");
            }

            if (!string.IsNullOrEmpty(checkComboBoxEnable.Text))
            {
                sb.Append(" and (");
                foreach (string str in checkComboBoxEnable.SelectedValue.Split(','))
                    sb.Append("WeiXiuDuiXiang=" + (int)Enum.Parse(typeof(Table.PurchaseOrderS.EnumEnable), str) + " or ");

                sb.Remove(sb.Length - 3, 3);
                sb.Append(")");
            }
            uCdataGrid1.Init(FCNS.Data.SQLdata.GetDataRows(sb.ToString()));
        }
    }
}